//获取所有的图书信息
window.onload = function(){
    //获取token
    const token = sessionStorage.getItem("token")
    //发送ajax
    axios({
        url:"http://localhost:8080/admin/book_info/queryAll",
        method:"post",
        headers:{token}
    })
    .then(res=>{
        if(res.data.code == 200){
            //分页
            //默认显示第一页
            sessionStorage.setItem("pageNo",1)
            //一页显示8条
            sessionStorage.setItem("pageNum",8)
            //将所有的图书信息放到浏览器内存里
            sessionStorage.setItem("BookInfo",JSON.stringify(res.data.data))
            studentRender(res.data.data)
        }else{
            alert("长时间不登录,请重新登录")
            location.href = "../html/login.html"
        }
    })
    //获取数据,将渲染封装渲染
}

//封装
function studentRender(data){
    const list = data.slice(0,8)
    const tbody = document.querySelector(".table-group-divider")
    tbody.innerHTML = ""
    //渲染
    list.forEach((item,index)=>{
        let str = `
        <tr align="center">
                <th scope="row">${index+1}</th>
                <td>${item.name}</td>
                <td>${item.author}</td>
                <td>${item.publish}</td>
                <td>${item.introduction}</td>
                <td>${item.language}</td>
                <td>${item.price}</td>
                <td>${item.pubdate}</td>
                <td><button class="btn btn-danger btn-sm" data-id=${item.book_id}>删除</button>
                <button class="btn btn-warning btn-sm" data-id=${item.book_id} data-bs-toggle="modal" data-bs-target="#upbookinfo">修改</button></td>
             </tr>
        `
        tbody.innerHTML += str;
    })
}

//删除操作
const tbody = document.querySelector(".table-group-divider")
tbody.addEventListener("click", (e) => {
    //删除图书
    if (e.target.className.includes('danger')) {
        const res = window.confirm("您确定要删除吗?")
        if (res) {
            //获取token
            const token = sessionStorage.getItem("token")
            const book_id = e.target.dataset.id
            axios({
                url: "http://localhost:8080/admin/book_info/del",
                method: "post",
                headers: { token },
                data: {
                    book_id
                }
            })
                .then(res => {
                    location.reload();
                })
        }
    }


    if (e.target.className.includes("warning")) {    
        const book_id = e.target.dataset.id
        const token = sessionStorage.getItem("token")
        axios({
            url: "http://localhost:8080/admin/book_info/queryOne",
            method: "post",
            headers: { token },
            data: { book_id }
        })
            .then(res => {
                console.log(res.data);
                //将服务器响应数据渲染到模态框里面去
                document.getElementById("ubook_id").value = res.data.data.book_id;
                document.getElementById("uname").value = res.data.data.name;
                document.getElementById("uauthor").value = res.data.data.author;
                document.getElementById("upublish").value = res.data.data.publish;
                document.getElementById("uintroduction").value = res.data.data.introduction;
                document.getElementById("ulanguage").value = res.data.data.language;
                document.getElementById("uprice").value = res.data.data.price;
                document.getElementById("upubdate").value = res.data.data.pubdate;
            })
    }
    
})

//添加图书信息
function AddBook_info() {
    //获取数据
    const name = document.getElementById("name").value.trim();
    const author = document.getElementById("author").value.trim();
    const publish = document.getElementById("publish").value.trim();
    const introduction= document.getElementById("introduction").value.trim();
    const language = document.getElementById("language").value.trim();
    const price= document.getElementById("price").value.trim();
    const pubdate= document.getElementById("pubdate").value.trim();
    //发送请求
    const token = sessionStorage.getItem("token")
    axios({
        url: "http://localhost:8080/admin/book_info/add",
        method: "post",
        headers: { token },
        data: {
            name, author,publish, introduction,language,price, pubdate
        }
    })
    .then(res => {
        location.reload();
    })
}

//修改学生信息
function UpBook_iInfo() {
    const book_id=document.getElementById("ubook_id").value
    const name = document.getElementById("uname").value
    const author = document.getElementById("uauthor").value
    const publish = document.getElementById("upublish").value
    const introduction= document.getElementById("uintroduction").value
    const language = document.getElementById("ulanguage").value
    const price= document.getElementById("uprice").value
    const pubdate= document.getElementById("upubdate").value
    //非空判断,或者正则校验

    axios({
        url: "http://localhost:8080/admin/book_info/update",
        method: "post",
        headers:{token},
        data: { book_id, name, author, publish,  introduction, language, price,pubdate }
    })
    .then(res=>{
        location.reload();
    })
}


//根据图书信息进行模糊查询
function SearchName(){
    const searchName=document.getElementById("searchName").value
    const token = sessionStorage.getItem("token")
    axios({
        url:"http://localhost:8080/admin/book_info/likename",
        method:"post",
        headers:{token},
        data:{searchName}
        
    })
    .then(res=>{
        //默认第一页,一页显示8个
        sessionStorage.setItem("pageNo",1)
        sessionStorage.setItem("pageNum",8)
        //渲染
        console.log(res.data.data);
        studentRender(res.data.data)

    })
}

function firstPage(){
    /* 
        1. 设置当前页为第一页,获取一页显示的个数
        2. 计算出当前显示的数量
        3. 渲染计算出来的结果
    */
    // 设置当前页为第一页,获取一页显示的个数
    sessionStorage.setItem("pageNo",1)
    const pageNum = sessionStorage.getItem("pageNum")
    const pageNo = sessionStorage.getItem("pageNo")
    // 获取图书信息
    const BookInfo = JSON.parse(sessionStorage.getItem("BookInfo"))
    const list = BookInfo.slice((pageNo-1)*pageNum,(pageNo*pageNum))
    //渲染数据
    studentRender(list)
}
function upPage(){
    /* 
        1. 获取当前页,获取一页显示多少条
        2. 将当前页-1,将当前再存储
        3. 计算显示的数据
        4. 渲染
    */
    const BookInfo = JSON.parse(sessionStorage.getItem("BookInfo"))
    let pageNo = +sessionStorage.getItem("pageNo")
    const pageNum = +sessionStorage.getItem("pageNum")
    pageNo = pageNo - 1;
    if(pageNo <= 1) pageNo = 1;
    sessionStorage.setItem("pageNo",pageNo)
    //渲染操作
    const list = BookInfo.slice((pageNo-1)*pageNum,(pageNo*pageNum))
    studentRender(list)
}
function downPage(){
    /* 
        1. 获取当前页,获取一页显示多少条
        2. 将当前页+1,将当前再存储
        3. 计算显示的数据
        4. 渲染
    */
    const BookInfo = JSON.parse(sessionStorage.getItem("BookInfo"))
    let pageNo = +sessionStorage.getItem("pageNo")
    const pageNum = +sessionStorage.getItem("pageNum")
    pageNo = pageNo + 1;
    //如果当前页大于最后一页,那么当前页就等于最后一页
    if(pageNo >= Math.ceil((BookInfo.length)/pageNum)) pageNo = Math.ceil((BookInfo.length)/pageNum);
    sessionStorage.setItem("pageNo",pageNo)
    //渲染操作
    const list = BookInfo.slice((pageNo-1)*pageNum,(pageNo*pageNum))
    studentRender(list)
}
function lastPage(){
    const BookInfo = JSON.parse(sessionStorage.getItem("BookInfo"))
    const pageNum = +sessionStorage.getItem("pageNum")
    const pageNo = Math.ceil((BookInfo.length)/pageNum)
    //当我们点击最后一页,那么当前页就是最后一页
    sessionStorage.setItem("pageNo",pageNo)
    //最后一页的数据
    const list = BookInfo.slice((pageNo-1)*pageNum,(pageNo*pageNum))
    studentRender(list)
}
//改变一页显示的数量
const changePageNum = document.getElementById("changePageNum")
changePageNum.onchange = function(){
    /* 
        1. 获取value,
        2. 将pageNum赋值成value
        3. 渲染
    */
    const BookInfo = JSON.parse(sessionStorage.getItem("BookInfo"))
    const pageNum = +this.value;
    //当改变一页显示的数量之后,那么就默认显示第一页
    const pageNo = 1;
    sessionStorage.setItem("pageNum",pageNum)
    sessionStorage.setItem("pageNo",pageNo)
    const list = BookInfo.slice((pageNo-1)*pageNum,(pageNo*pageNum))
    studentRender(list)
}

